/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RegexEditor.java
 *
 * Created on Nov 22, 2010, 12:23:59 PM
 */
package Relationship.Complex.Creation;

import ResultEditor.AnnotationClasses.Depot;
import Relationship.Complex.DataTypes.ComplexRel;
import java.awt.Color;

/**
 * This Graphical User Interface will provide a way for users to create Complex
 * Relationship entries for the schema.
 * <pre>
 * Rules for allowing Complex Relationships into the Schema.
 *  1)Relationship name must be unique in the schema
 *  2)Regular expression must be unique in the schema
 *  3)Regular expression must compile
 *  4)Regular Expression meets the requirements specified in the 'help' section.
 *      a)Only uses class names within the schema and regex symbols
 *      b)All classes are in parantheses (Test) not Test
 * <pre>
 *
 * @author Kyle
 */
public class RegexEditor extends javax.swing.JFrame
{
    //<editor-fold defaultstate="collapsed" desc="Member Variables">

    //Member Variables
    private ComplexRel rel;
    /*
     * The GUI that called the Regex Editor
     * TODO: Might want to replace this with an interface so this class can be called
     * from other classes...
     */
    private ComplexSchemaManager caller;
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Constructors">
    /** Creates new form RegexEditor */
    public RegexEditor(ComplexRel existing, Relationship.Complex.Creation.ComplexSchemaManager editor)
    {
        //Initialize components
        initComponents();

        //If they passed in a non-null Complex Relationship place its' info in the text boxes.
        if (existing != null)
        {
            regex.setText(existing.getAllowed());
            name.setText(existing.getName());
        }

        //Keep track of the GUI that called this one
        caller = editor;

        //Get Classes to show user
        Depot depot = new Depot();
        String[] classes = depot.getAnnotationClasssnamesString();

        //Make the Classes regex safe(add a forward slash before any regex symbol)
        classes = VerifyRegexWithClasses.Regexerate(classes, "\\\\");

        //Display Classes so users know what they can use
        this.jList1.setListData(classes);

        //Initial set up of buttons/ set background color of text boxes
        this.updateBasedOnParamValidity();
        
        setDialogLocation();
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Private Methods">
    /**
     * This method should be called whenever the name or regular expression change.
     * This method will enable the save button if the name and regular expression are valid
     * and disable otherwise.
     */
    private void updateBasedOnParamValidity()
    {
        //Verify that the regular expression and name that the user entered is valid
        boolean goodRegex = VerifyRegexWithClasses.CheckValidRegex(regex.getText()) && !regex.getText().equals("");
        boolean goodName = !name.getText().equals("");

        //Verify that the relationship name and regular expression are unique to the schema.
        boolean nameExists = env.Parameters.ComplexRelationshipNames.checkNameExists(name.getText());
        boolean regexExists = env.Parameters.ComplexRelationshipNames.checkRegexExists(regex.getText());

        //Concatenate boolean results to be more useful
        boolean regexReady = goodRegex && !regexExists;
        boolean nameReady = goodName && !nameExists;
        boolean everythingReady = nameReady & regexReady;

        //If everything is good enable the 'next' button and set background color
        //of regex bar to green.
        if(everythingReady)
        {
            
            regex.setBackground(new Color(255,255,255));
            name.setBackground( new Color(255,255,255));
            save.setEnabled(true);
        }

        //Regex is bad or already exists in schema...
        else
        {
            
            if(!regexReady)
                regex.setBackground(new Color(255,150,150));
            else
                regex.setBackground(new Color(255,255,255));
            if(!nameReady)
                name.setBackground(new Color(255, 150, 150));
            else
                name.setBackground(new Color(255,255,255));
             
            save.setEnabled(false);
        }
    }
    /**
     * Center this dialog in callers window.
     */
    private void setDialogLocation()
    {
        //If null was passed into the constructor then just return
        if (caller == null)
        {
            return;
        }

        //If non null gui then center the gui in the Callers window.
        int parentX = caller.getX(), parentY = caller.getY();
        int parentWidth = caller.getWidth(), parentHeight = caller.getHeight();
        int width = this.getWidth(), height = this.getHeight();

        int x = parentX + (int) ((parentWidth - width) / 2);
        int y = parentY + (int) ((parentHeight - height) / 2);

        this.setLocation(x, y);
    }
    //</editor-fold>
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        regex = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jPanel13 = new javax.swing.JPanel();
        name = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        valid = new javax.swing.JLabel();
        jPanel9 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        jPanel11 = new javax.swing.JPanel();
        help = new javax.swing.JButton();
        save = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel12 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Complex Relationship Editor");

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.GridLayout(3, 0, 10, 10));

        jPanel3.setLayout(new java.awt.BorderLayout());

        regex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                regexActionPerformed(evt);
            }
        });
        regex.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                regexInputMethodTextChanged(evt);
            }
        });
        regex.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                regexKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                regexKeyTyped(evt);
            }
        });
        jPanel3.add(regex, java.awt.BorderLayout.CENTER);

        jLabel1.setText("Regular Expression:");
        jPanel3.add(jLabel1, java.awt.BorderLayout.LINE_START);

        jPanel2.add(jPanel3);

        jPanel13.setLayout(new java.awt.BorderLayout());

        name.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameActionPerformed(evt);
            }
        });
        name.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                nameKeyReleased(evt);
            }
        });
        jPanel13.add(name, java.awt.BorderLayout.CENTER);

        jLabel3.setText(" Relationship Name:");
        jPanel13.add(jLabel3, java.awt.BorderLayout.LINE_START);

        jPanel2.add(jPanel13);

        jPanel5.setLayout(new java.awt.GridLayout(0, 8));

        jPanel6.setLayout(new java.awt.BorderLayout());
        jPanel5.add(jPanel6);

        jPanel7.setLayout(new java.awt.BorderLayout());
        jPanel5.add(jPanel7);

        jPanel8.setLayout(new java.awt.BorderLayout());
        jPanel8.add(valid, java.awt.BorderLayout.CENTER);

        jPanel5.add(jPanel8);

        jPanel9.setLayout(new java.awt.BorderLayout());
        jPanel5.add(jPanel9);

        jPanel10.setLayout(new java.awt.BorderLayout());
        jPanel5.add(jPanel10);

        jPanel11.setLayout(new java.awt.BorderLayout());

        help.setText("Help");
        help.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpActionPerformed(evt);
            }
        });
        jPanel11.add(help, java.awt.BorderLayout.CENTER);

        jPanel5.add(jPanel11);

        save.setText("Save");
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });
        jPanel5.add(save);

        cancel.setText("Cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });
        jPanel5.add(cancel);

        jPanel2.add(jPanel5);

        jPanel1.add(jPanel2, java.awt.BorderLayout.PAGE_END);

        jPanel4.setLayout(new java.awt.BorderLayout());

        jList1.setBackground(new java.awt.Color(204, 204, 204));
        jScrollPane1.setViewportView(jList1);

        jPanel4.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel12.setBackground(new java.awt.Color(0, 153, 204));
        jPanel12.setForeground(new java.awt.Color(0, 153, 204));

        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Available Classes: ONLY USE THESE STRINGS AND REGEX CHARACTERS!");
        jPanel12.add(jLabel2);

        jPanel4.add(jPanel12, java.awt.BorderLayout.PAGE_START);

        jPanel1.add(jPanel4, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // <editor-fold defaultstate="collapsed" desc="Events">
    /**
     * 'Save' button pressed, save the relationship and dispose of this GUI.
     * @param evt
     */
    private void saveActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_saveActionPerformed
    {//GEN-HEADEREND:event_saveActionPerformed
        rel = new ComplexRel(name.getText(), regex.getText());
        env.Parameters.ComplexRelationshipNames.add(rel);
        caller.updateRelationships();
        this.dispose();
    }//GEN-LAST:event_saveActionPerformed

    /**
     * 'Help' button pressed, show help information.
     * @param evt
     */
    private void helpActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_helpActionPerformed
    {//GEN-HEADEREND:event_helpActionPerformed
        ComplexRelationshipInfo info = new ComplexRelationshipInfo(this);
        info.setVisible(true);
    }//GEN-LAST:event_helpActionPerformed

    private void regexActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_regexActionPerformed
    {//GEN-HEADEREND:event_regexActionPerformed
    }//GEN-LAST:event_regexActionPerformed

    /**
     * User changed regular expression.. update gui based on validity of new
     * regular expression.
     * @param evt
     */
    private void regexKeyReleased(java.awt.event.KeyEvent evt)//GEN-FIRST:event_regexKeyReleased
    {//GEN-HEADEREND:event_regexKeyReleased
        this.updateBasedOnParamValidity();;
    }//GEN-LAST:event_regexKeyReleased


    private void regexKeyTyped(java.awt.event.KeyEvent evt)//GEN-FIRST:event_regexKeyTyped
    {//GEN-HEADEREND:event_regexKeyTyped
    }//GEN-LAST:event_regexKeyTyped
    /**
     * User canceled this action, dispose gui.
     * @param evt
     */
    private void cancelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelActionPerformed
    {//GEN-HEADEREND:event_cancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelActionPerformed

    private void regexInputMethodTextChanged(java.awt.event.InputMethodEvent evt)//GEN-FIRST:event_regexInputMethodTextChanged
    {//GEN-HEADEREND:event_regexInputMethodTextChanged
    }//GEN-LAST:event_regexInputMethodTextChanged

    private void nameActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_nameActionPerformed
    {//GEN-HEADEREND:event_nameActionPerformed

    }//GEN-LAST:event_nameActionPerformed

    /**
     * User changed name, update gui based on validity of new name
     * @param evt
     */
    private void nameKeyReleased(java.awt.event.KeyEvent evt)//GEN-FIRST:event_nameKeyReleased
    {//GEN-HEADEREND:event_nameKeyReleased
        this.updateBasedOnParamValidity();
    }//GEN-LAST:event_nameKeyReleased
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="GUI Component Declaration">
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancel;
    private javax.swing.JButton help;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField name;
    private javax.swing.JTextField regex;
    private javax.swing.JButton save;
    private javax.swing.JLabel valid;
    // End of variables declaration//GEN-END:variables
    //</editor-fold>
}
